package org.dromara.lessor.sales.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.admin.constant.HouseGlobalConstant;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.lessor.domain.bo.HouseBo;
import org.dromara.lessor.domain.vo.HouseVo;
import org.dromara.lessor.service.IHouseService;
import org.dromara.system.mapper.SysUserRoleMapper;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 业务员办单控制层
 *
 * @author Lion Li
 * @date 2025-09-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/sales")
public class SalesController extends BaseController {

    private final IHouseService houseService;
    private final SysUserRoleMapper userRoleMapper;

    /**
     * 业务员获取所属出租方房源列表
     */
//    @SaCheckPermission("sale:house:list")
    @GetMapping("/myHouseList")
    public TableDataInfo<HouseVo> myHouseList(HouseBo bo, PageQuery pageQuery) {
        Long userId = LoginHelper.getUserId();
        if (userId == null) {
            throw new ServiceException("用户未登录");
        }
        return houseService.queryPageMyHouseList(bo, pageQuery, userId);
    }

    /**
     * 业务员查询自己办理的房源订单
     */
//    @SaCheckPermission("sale:house:list")
    @GetMapping("/myOrderList")
    public TableDataInfo<HouseVo> myOrderList(HouseBo bo, PageQuery pageQuery) {
        Long userId = LoginHelper.getUserId();
        if (userId == null) {
            throw new ServiceException("用户未登录");
        }
        return houseService.queryPageMyOrderList(bo, pageQuery, userId);
    }

    /**
     * 业务员获取房源信息详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("sale:house:query")
    @GetMapping("/{id}")
    public R<HouseVo> getInfo(@NotNull(message = "主键不能为空")
                              @PathVariable Long id) {
        return R.ok(houseService.queryById(id));
    }

    /**
     * 业务员办单
     */
//    @SaCheckPermission("sale:house:handleRent")
    @PostMapping("/handleRent")
    public R<Void> handleRent(@RequestBody @Validated HouseBo houseBo) {
        Long userId = LoginHelper.getUserId();
        if (userId == null) {
            throw new ServiceException("用户未登录");
        }
        List<Long> userIdsByRoleId = userRoleMapper.selectUserIdsByRoleId(HouseGlobalConstant.SALE_ROLE_ID);
        if (!userIdsByRoleId.contains(userId)) {
            throw new ServiceException("登陆的账号不是业务员账号！");
        }
        houseBo.setSaleId(userId);
        return R.ok(houseService.handleRent(houseBo));
    }

    /**
     * 业务员办单后取消订单
     */
//    @SaCheckPermission("sale:house:handleRent")
    @PostMapping("/cancel/{houseId}")
    public R<Void> saleCancel(@NotNull(message = "房源id不能为空") @PathVariable("houseId") Long houseId) {
        return toAjax(houseService.saleCancel(houseId));
    }

}
